// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>

#include <nebula/types/type_fwd.h>
#include <turbo/base/macros.h>

namespace nebula {
namespace util {

// Convert a UTF8 string to a wstring (either UTF16 or UTF32, depending
// on the wchar_t width).
TURBO_EXPORT turbo::Result<std::wstring> UTF8ToWideString(std::string_view source);

// Similarly, convert a wstring to a UTF8 string.
TURBO_EXPORT turbo::Result<std::string> WideStringToUTF8(const std::wstring& source);

// Convert UTF8 string to a UTF16 string.
TURBO_EXPORT turbo::Result<std::u16string> UTF8StringToUTF16(std::string_view source);

// Convert UTF16 string to a UTF8 string.
TURBO_EXPORT turbo::Result<std::string> UTF16StringToUTF8(std::u16string_view source);

// This function needs to be called before doing UTF8 validation.
TURBO_EXPORT void InitializeUTF8();

TURBO_EXPORT bool validate_utf8(const uint8_t* data, int64_t size);

TURBO_EXPORT bool validate_utf8(std::string_view str);

// Skip UTF8 byte order mark, if any.
TURBO_EXPORT
turbo::Result<const uint8_t*> SkipUTF8BOM(const uint8_t* data, int64_t size);

static constexpr uint32_t kMaxUnicodeCodepoint = 0x110000;

}  // namespace util
}  // namespace nebula
